package com.taplinker.client.crm.rpc;


import android.widget.Toast;

import com.google.protobuf.InvalidProtocolBufferException;
import com.taplinker.client.AppConfigContext;
import com.taplinker.client.crm.domain.CrmDataCenter;
import com.taplinker.client.crm.domain.CustomerInfo;
import com.taplinker.core.rpc.Result;
import com.taplinker.core.rpc.command.SimpleBaseCommand;
import com.taplinker.core.rpc.http.HttpTemplate;
import com.taplinker.dto.web.ActionCusRequestProto;
import com.taplinker.dto.web.ActionCusRequestProto.ActionCusRequest.Builder;
import com.taplinker.dto.web.ActionCusRequestProto.ActionCusRequestType;
import com.taplinker.dto.web.ActionProto.Action;
/**
 * 获取用户的时间轴
 * @author djbian
 *
 */
public class GetCustomerTimeLine  extends SimpleBaseCommand {

	private ActionCusRequestType type;
	private CustomerInfo info;
	public GetCustomerTimeLine(ActionCusRequestType type,CustomerInfo info,String date)
	{
		this.type = type;
		this.info = info;
	}
	
	public GetCustomerTimeLine(CustomerInfo info)
	{
		this.type = ActionCusRequestType.ALL;
		this.info = info;
	}
	@Override
	public Result go() {
		Builder b = ActionCusRequestProto.ActionCusRequest.newBuilder();
		b.setType(type);
		if(info.getCustomer()!=null)
			b.setVisitorId(info.getCustomer().getVistorId());
		else
			b.setVisitorId(info.getVisitorId());
		b.setEndDate("");
		b.setLimit(10);
		if(info != null&& info.getCustomerAction().size()>0)
			b.setEndDate(String.valueOf(info.getCustomerAction().get(info.getCustomerAction().size()-1).getDate()));
		return new HttpTemplate().postForGogoByte(AppConfigContext.TAPLINKER_WEB
				+ "/services/crm/v1/action/customer", b.build().toByteArray());
	}

	@Override
	public void yes(Result result) {
		super.yes(result);
		try {
			Action action = Action.parseFrom((byte[])result.object);
			info.addCustomerAction(action.getCActionList());
			CrmDataCenter.getInstance().noticeListener(CrmDataCenter.GET_CUSTOMER_TIMELINE);
		} catch (InvalidProtocolBufferException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void no(Result result) {
		Toast.makeText(CrmDataCenter.getInstance().getContext(), "" + result.message, Toast.LENGTH_SHORT).show();
	}
}